{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第七讲：求解$Ax=0$，主变量，特解\n",
    "\n",
    "举例：$3 \\times 4$矩阵\n",
    "$\n",
    "A=\n",
    "\\begin{bmatrix}\n",
    "1 & 2 & 2 & 2\\\\\n",
    "2 & 4 & 6 & 8\\\\\n",
    "3 & 6 & 8 & 10\\\\\n",
    "\\end{bmatrix}\n",
    "$，求$Ax=0$的特解：\n",
    "\n",
    "找出主变量（pivot variable）：\n",
    "$$\n",
    "A=\n",
    "\\begin{bmatrix}\n",
    "1 & 2 & 2 & 2\\\\\n",
    "2 & 4 & 6 & 8\\\\\n",
    "3 & 6 & 8 & 10\\\\\n",
    "\\end{bmatrix}\n",
    "\\underrightarrow{消元}\n",
    "\\begin{bmatrix}\n",
    "\\underline{1} & 2 & 2 & 2\\\\\n",
    "0 & 0 & \\underline{2} & 4\\\\\n",
    "0 & 0 & 0 & 0\\\\\n",
    "\\end{bmatrix}\n",
    "=U\n",
    "$$\n",
    "\n",
    "主变量（pivot variable，下划线元素）的个数为2，即矩阵$A$的秩（rank）为2，即$r=2$。\n",
    "\n",
    "主变量所在的列为主列（pivot column），其余列为自由列（free column）。\n",
    "\n",
    "自由列中的变量为自由变量（free variable），自由变量的个数为$n-r=4-2=2$。\n",
    "\n",
    "通常，给自由列变量赋值，去求主列变量的值。如，令$x_2=1, x_4=0$求得特解\n",
    "$x=c_1\\begin{bmatrix}-2\\\\1\\\\0\\\\0\\\\\\end{bmatrix}$；\n",
    "再令$x_2=0, x_4=1$求得特解\n",
    "$x=c_2\\begin{bmatrix}2\\\\0\\\\-2\\\\1\\\\\\end{bmatrix}$。\n",
    "\n",
    "该例还能进一步简化，即将$U$矩阵化简为$R$矩阵（Reduced row echelon form），即简化行阶梯形式。\n",
    "\n",
    "在简化行阶梯形式中，主元上下的元素都是$0$：\n",
    "$$\n",
    "U=\n",
    "\\begin{bmatrix}\n",
    "\\underline{1} & 2 & 2 & 2\\\\\n",
    "0 & 0 & \\underline{2} & 4\\\\\n",
    "0 & 0 & 0 & 0\\\\\n",
    "\\end{bmatrix}\n",
    "\\underrightarrow{化简}\n",
    "\\begin{bmatrix}\n",
    "\\underline{1} & 2 & 0 & -2\\\\\n",
    "0 & 0 & \\underline{1} & 2\\\\\n",
    "0 & 0 & 0 & 0\\\\\n",
    "\\end{bmatrix}\n",
    "=R\n",
    "$$\n",
    "\n",
    "将$R$矩阵中的主变量放在一起，自由变量放在一起（列交换），得到\n",
    "\n",
    "$$\n",
    "R=\n",
    "\\begin{bmatrix}\n",
    "\\underline{1} & 2 & 0 & -2\\\\\n",
    "0 & 0 & \\underline{1} & 2\\\\\n",
    "0 & 0 & 0 & 0\\\\\n",
    "\\end{bmatrix}\n",
    "\\underrightarrow{列交换}\n",
    "\\left[\n",
    "\\begin{array}{c c | c c}\n",
    "1 & 0 & 2 & -2\\\\\n",
    "0 & 1 & 0 & 2\\\\\n",
    "\\hline\n",
    "0 & 0 & 0 & 0\\\\\n",
    "\\end{array}\n",
    "\\right]\n",
    "=\n",
    "\\begin{bmatrix}\n",
    "I & F \\\\\n",
    "0 & 0 \\\\\n",
    "\\end{bmatrix}\n",
    "\\textrm{，其中}I\\textrm{为单位矩阵，}F\\textrm{为自由变量组成的矩阵}\n",
    "$$\n",
    "\n",
    "计算零空间矩阵$N$（nullspace matrix），其列为特解，有$RN=0$。\n",
    "\n",
    "$$\n",
    "x_{pivot}=-Fx_{free} \\\\\n",
    "\\begin{bmatrix}\n",
    "I & F \\\\\n",
    "\\end{bmatrix}\n",
    "\\begin{bmatrix}\n",
    "x_{pivot} \\\\\n",
    "x_{free} \\\\\n",
    "\\end{bmatrix}=0 \\\\\n",
    "N=\\begin{bmatrix}\n",
    "-F \\\\\n",
    "I \\\\\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "在本例中\n",
    "$\n",
    "N=\n",
    "\\begin{bmatrix}\n",
    "-2 & 2 \\\\\n",
    "0 & -2 \\\\\n",
    "1 & 0 \\\\\n",
    "0 & 1 \\\\\n",
    "\\end{bmatrix}\n",
    "$，与上面求得的两个$x$特解一致。\n",
    "\n",
    "另一个例子，矩阵\n",
    "$\n",
    "A=\n",
    "\\begin{bmatrix}\n",
    "1 & 2 & 3 \\\\\n",
    "2 & 4 & 6 \\\\\n",
    "2 & 6 & 8 \\\\\n",
    "2 & 8 & 10 \\\\\n",
    "\\end{bmatrix}\n",
    "\\underrightarrow{消元}\n",
    "\\begin{bmatrix}\n",
    "1 & 2 & 3 \\\\\n",
    "0 & 2 & 2 \\\\\n",
    "0 & 0 & 0 \\\\\n",
    "0 & 0 & 0 \\\\\n",
    "\\end{bmatrix}\n",
    "\\underrightarrow{化简}\n",
    "\\begin{bmatrix}\n",
    "1 & 0 & 1 \\\\\n",
    "0 & 1 & 1 \\\\\n",
    "0 & 0 & 0 \\\\\n",
    "0 & 0 & 0 \\\\\n",
    "\\end{bmatrix}\n",
    "=R\n",
    "$\n",
    "\n",
    "矩阵的秩仍为$r=2$，有$2$个主变量，$1$个自由变量。\n",
    "\n",
    "同上一例，取自由变量为$x_3=1$，求得特解\n",
    "$\n",
    "x=c\n",
    "\\begin{bmatrix}\n",
    "-1 \\\\\n",
    "-1 \\\\\n",
    "1 \\\\\n",
    "\\end{bmatrix}\n",
    "$"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
